/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmTrangThaiHoaDon.java
 *
 * Created on Aug 29, 2012, 1:44:57 PM
 * Tên: Nguyễn Thị Ngọc Cẩm 
 * Lớp: 11HCB
 * MSSV: 1142006
 * 
 */
package gui;

import dao.*;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;
import pojo.*;

/**
 *
 * @author Tên: Nguyễn Thị Ngọc Cẩm Lớp: 11HCB MSSV: 1142006
 *
 */
public class frmTrangThaiHoaDon extends javax.swing.JFrame {

    private Cuahang _ch;
    private List<Hoadon> _lhd;
    private List<Chitiethoadon> _lcthd;

    /**
     * Creates new form frmTrangThaiHoaDon
     */
    public frmTrangThaiHoaDon() {
        initComponents();
        this._ch = new Cuahang();
        this._ch.setMacuahang(1);
        this._ch.setTencuahang("Cua hang Tap hoa 1");

        this._lhd = HoaDonDAO.layDanhSachHoaDon(this._ch);
        fillJcbxCTHD();
        this._lcthd = ChiTietHoaDonDAO.layDanhSachChiTietHoaDon(this._lhd.get(0));
        fileJTableChiTietHoaDon();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jComboBox_DSMaHoaDon = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jlblTrangThai = new javax.swing.JLabel();
        jlblTenKhachHang = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jlblTongTien = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable_TrangThaiHD = new javax.swing.JTable();
        jbtn_Thoat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin theo dõi trạng thái hóa đơn", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 1, 14), java.awt.Color.blue)); // NOI18N

        jLabel2.setText("Mã Hóa Đơn");

        jButton1.setText("jButton1");

        jComboBox_DSMaHoaDon.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[Chọn mã HD]", "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox_DSMaHoaDon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_DSMaHoaDonActionPerformed(evt);
            }
        });

        jLabel3.setText("Tên Khách Hàng");

        jLabel4.setText("Trạng Thái Hóa Đơn");

        jlblTrangThai.setBackground(new java.awt.Color(255, 51, 102));
        jlblTrangThai.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jlblTrangThai.setForeground(new java.awt.Color(255, 0, 0));
        jlblTrangThai.setText("Đang Thanh Toán");

        jlblTenKhachHang.setBackground(new java.awt.Color(255, 51, 102));
        jlblTenKhachHang.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jlblTenKhachHang.setText("Ten Khach hang");

        jLabel5.setText("Tổng Tiền");

        jlblTongTien.setBackground(new java.awt.Color(255, 51, 102));
        jlblTongTien.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jlblTongTien.setText("Tien");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlblTongTien)
                    .addComponent(jComboBox_DSMaHoaDon, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblTenKhachHang)
                    .addComponent(jlblTrangThai))
                .addGap(186, 186, 186))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox_DSMaHoaDon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblTenKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblTrangThai, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblTongTien, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(0, 0, 102));
        jLabel7.setText("XEM TÌNH TRẠNG  HÓA ĐƠN");

        jTable_TrangThaiHD.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTable_TrangThaiHD);

        jbtn_Thoat.setText("Thoát");
        jbtn_Thoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtn_ThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 583, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jbtn_Thoat, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28))
            .addGroup(layout.createSequentialGroup()
                .addGap(180, 180, 180)
                .addComponent(jLabel7)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbtn_Thoat, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
//<editor-fold defaultstate="collapsed" desc="comment">
private void jbtn_ThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtn_ThoatActionPerformed
// TODO add your handling code here:
    this.setVisible(false);
}//GEN-LAST:event_jbtn_ThoatActionPerformed
    int mact = 0;
private void jComboBox_DSMaHoaDonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_DSMaHoaDonActionPerformed
// TODO add your handling code here:

    int vitri = jComboBox_DSMaHoaDon.getSelectedIndex();
    this.jlblTenKhachHang.setText(this._lhd.get(vitri).getKhachhang().getTenkhachhang());
    boolean trangThaiHD = this._lhd.get(vitri).getTrangthai();
    if (trangThaiHD) {
        this.jlblTrangThai.setText("Đã thanh toán");
    } else {
        this.jlblTrangThai.setText("Đang chờ thanh toán");
    }

    this._lcthd = ChiTietHoaDonDAO.layDanhSachChiTietHoaDon(this._lhd.get(vitri));
    fileJTableChiTietHoaDon();
}//GEN-LAST:event_jComboBox_DSMaHoaDonActionPerformed

    //Lay du lieu tu DB DS Cua Hang dua vao combobox
    private void fillJcbxCTHD() {
        DefaultComboBoxModel cbxModel = new DefaultComboBoxModel();
        this.jComboBox_DSMaHoaDon.setModel(cbxModel);
        for (int i = 0; i < _lhd.size(); i++) {
            cbxModel.addElement(_lhd.get(i).getMahoadon());
        }
    }

    private void fileJTableChiTietHoaDon() {
        double tongTien = 0;
        DefaultTableModel tableModel = new DefaultTableModel();
        tableModel.addColumn("Tên Sản Phẩm");
        tableModel.addColumn("Đơn Giá");
        tableModel.addColumn("Số lượng");
        tableModel.addColumn("Thành tiền");
        this.jTable_TrangThaiHD.setModel(tableModel);
        for (int i = 0; i < this._lcthd.size(); i++) {
            Vector vt = new Vector();
            vt.add(this._lcthd.get(i).getSanpham().getTensanpham());
            vt.add(this._lcthd.get(i).getDongia());
            vt.add(this._lcthd.get(i).getSoluong());
            Double thanhTien = this._lcthd.get(i).getDongia() * this._lcthd.get(i).getSoluong();
            vt.add(thanhTien);
            tongTien += thanhTien;
            tableModel.addRow(vt);
        }
        this.jlblTongTien.setText(String.valueOf(tongTien));
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmTrangThaiHoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmTrangThaiHoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmTrangThaiHoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmTrangThaiHoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new frmTrangThaiHoaDon().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox_DSMaHoaDon;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable_TrangThaiHD;
    private javax.swing.JButton jbtn_Thoat;
    private javax.swing.JLabel jlblTenKhachHang;
    private javax.swing.JLabel jlblTongTien;
    private javax.swing.JLabel jlblTrangThai;
    // End of variables declaration//GEN-END:variables
}
